<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="问卷名称" prop="pId">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入问卷名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="surveysList"
      row-key="surveyId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="问卷标题/问题内容/选项内容" prop="title" />
      <el-table-column label="问卷描述" align="center" prop="description" />
      <el-table-column label="问卷开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="问卷结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            v-if="scope.row.isActive!=9"
            @click="handleAdd(scope.row)"
          >{{ scope.row.pId==0?'新增问题':'新增选项' }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改问卷对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="190px">
        <el-form-item label="问卷标题/问题内容/选项内容" prop="title">
          <el-input v-model="form.title" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item v-if="form.pId==0" label="问卷描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item v-if="form.pId==0" label="问卷开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择问卷开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="form.pId==0" label="问卷结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择问卷结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="form.pId!=0" label="问题类型" prop="">
          <el-select v-model="form.questionType"  placeholder="请选择">
                <el-option   label="单选题" value="单选题"  ></el-option>
                <el-option   label="多选题" value="多选题"  ></el-option>
                <!-- <el-option   label="文本题" value="文本题"  ></el-option> -->
              </el-select>
        </el-form-item>
        <el-form-item v-if="form.pId!=0&&form.isActive!=9" label="补充选项" prop="description">
          <el-input v-model="form.remark"  placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="上级" prop="pId">
          <treeselect style="pointer-events: none;" v-model="form.pId" :options="surveysOptions" :normalizer="normalizer" placeholder="请选择" />
        </el-form-item>
        <el-form-item v-if='form.pId==0' label="积分" prop="pId">
          <el-input v-model="form.remark"  placeholder="请输入内容" />
        </el-form-item>
        <el-form-item v-if='form.pId==0' label="图片" prop="pId">
          <image-upload v-model="form.questionType"/>
        </el-form-item>
        <el-form-item  label="排序" prop="pId">
          <el-input v-model="form.sortOrder"  placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSurveys, getSurveys, delSurveys, addSurveys, updateSurveys } from "@/api/yxz/surveys";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Surveys",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 问卷表格数据
      surveysList: [],
      // 问卷树选项
      surveysOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        title: null,
        description: null,
        startTime: null,
        endTime: null,
        isActive: null,
        questionType: null,
        isRequired: null,
        sortOrder: null,
        pId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询问卷列表 */
    getList() {
      this.loading = true;
      listSurveys(this.queryParams).then(response => {
        if(response.data.length>0){
          this.surveysList = this.handleTree(response.data, "surveyId", "pId");
        }
        this.loading = false;
      });
    },
    /** 转换问卷数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.surveyId,
        label: node.title,
        children: node.children
      };
    },
	/** 查询问卷下拉树结构 */
    getTreeselect() {
      listSurveys().then(response => {
        this.surveysOptions = [];
        const data = { surveyId: 0, title: '问卷', children: [] };
        data.children = this.handleTree(response.data, "surveyId", "pId");
        this.surveysOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        surveyId: null,
        title: null,
        description: null,
        startTime: null,
        endTime: null,
        isActive: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        questionType: null,
        isRequired: null,
        sortOrder: null,
        pId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.surveyId) {
        this.form.pId = row.surveyId;
      } else {
        this.form.pId = 0;
      }
      if(row.pId!=0&&row.pId!=undefined){
        this.form.isActive='9'
      }
      this.open = true;
      this.title = "添加问卷";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.pId = row.pId;
      }
      getSurveys(row.surveyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问卷";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.surveyId != null) {
            updateSurveys(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSurveys(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除问卷编号为"' + row.surveyId + '"的数据项？').then(function() {
        return delSurveys(row.surveyId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
